<?php

# We require the library
//require("facebook.php");
include_once '../src/facebook.php';

# Creating the facebook object
$facebook = new Facebook(array(
    'appId'  => '151908311533018',
    'secret' => 'f5e3f2d39fdf14be7d448580e0e2b7d0',
    'cookie' => true
));

# Let's see if we have an active session
$session = $facebook->getSession();

if(!empty($session)) {
    # Active session, let's try getting the user id (getUser()) and user info (api->('/me'))
    /*try{
        $uid = $facebook->getUser();
        $user = $facebook->api('/me');
    } catch (Exception $e){}

    if(!empty($user)){
        # User info ok? Let's print it (Here we will be adding the login and registering routines)
        print_r($user);
    } else {
        # For testing purposes, if there was an error, let's kill the script
        die("There was an error.");
    }   */
	
	
	
	$uid = $facebook->getUser();
	$album_name="Bhoomikha";
	
	//At the time of writing it is necessary to enable upload support in the Facebook SDK, you do this with the line:
	$facebook->setFileUploadSupport(true);
	
	//Find existing album
	$album = $facebook->api(array(
	'query' => 'SELECT object_id,name FROM album WHERE owner='.$uid.' AND name=\''.$album_name.'\' AND can_upload=1',
	'method' => 'fql.query'
	)); 
	
	/*$albums = $facebook->api(array(
	'query' => 'SELECT aid,object_id,name FROM album WHERE owner='.$uid,
	'method' => 'fql.query'
	)); 
	
	foreach ($albums as $album)
		echo $album['aid']."  ".$album['object_id']."  ".$album['name'].'\n'; */
	
	if (isset($album[0]['object_id']))
	{
		$album_uid = $album[0]['object_id'];
	}
	else{   
		//Create an album
		$album_details = array(
        'message'=> 'PicSync',
        'name'=> $album_name
		);
	
		$create_album = $facebook->api('/'.$facebook->getUser().'/albums', 'post', $album_details);
	
		//Get album ID of the album you've just created
		$album_uid = $create_album['id'];
	}
	
	$photo_name='Bhomikha';
	//Upload a photo to album of ID...
	$photo_details = array(
    'message'=> $photo_name
	);
	
	$file='C:\\bhomikha.jpg'; //Example image file
	$photo_details['image'] = '@' . realpath($file);
	
	$upload_photo = $facebook->api('/'.$album_uid.'/photos', 'post', $photo_details);
	
	echo 'photo added successfully to the album';
	
	
} else {
    # There's no active session, let's generate one
    $login_url = $facebook->getLoginUrl(array(  
    //'req_perms' => 'email,user_birthday,status_update,publish_stream,user_photos,user_videos'));  
	'req_perms' => 'user_photos'));  
    header("Location: ".$login_url);	
}

	
?>